

package xib.parserTools.data;

import java.util.ArrayList;
import xib.parserTools.interfaces.IChapterData;
import xib.parserTools.interfaces.IPageData;
import xib.utils.EncodedString;
import xib.utils.XibDataException;

/**
 * Implementation from the interface IChapterData, this implementation is
 * used to save the data from the source before to be processed by the core.
 * @author xavierds
 */
public final class ChapterData implements IChapterData {

    /**
     * Field with the list of pages of the chapter.
     */
    private ArrayList<IPageData> pages;

    /**
     * Field width the list of titles in their language.
     */
    private ArrayList<EncodedString> titles;

    /**
     * Field with the number of the chapter.
     */
    private int number;

    /**
     * Field who indicates if the panel was validate.
     */
    private boolean validate;

    
    /**
     * Default Constructor. It initialize the list of pages and titles and sets
     * the others fields to null.
     */
    public ChapterData() {
        pages = new ArrayList<IPageData>();
        titles = new ArrayList<EncodedString>();
    }


    public ArrayList<EncodedString> getTitles(){
        if(titles.isEmpty())
            throwXibDataException(new IllegalStateException("Titles weren't " +
                    "initialized properly."));
            
        return titles;
    }


    public ArrayList<IPageData> getPages() {
        if(pages.isEmpty())
            throwXibDataException(new IllegalStateException("Pages weren't" +
                    " initialized properly."));
        return pages;
    }


    public int getChapterNumber() {
        return number;
    }


    public void addPage(IPageData pd) {
        validate = false;
        if(pd == null)
            throwXibDataException(new NullPointerException("The page pd is " +
                    "not initialized properly."));
        if(pages.contains(pd))
            throwXibDataException(new IllegalArgumentException("The " +
                    "page pd is already defined in this Chapter."));
        pages.add(pd);
    }


    public void addTitle(String title, String language) {
        validate = false;
        if(title == null || language == null)
            throwXibDataException(new NullPointerException("Title " +
                    "or language can not be null."));

        if(title.equals("") || language.equals(""))
            throwXibDataException(new IllegalArgumentException("Title " +
                    "or language can not be the empty String."));
        this.titles.add(new EncodedString(title,language));
    }


    public void setChapterNumber(int chapterNumber) {
        validate = false;
        number = chapterNumber;
    }

    //TODO -> validate() See if necesary to add the page position comprobation.
    public boolean validate(){
        if(!validate){
            //Titles list can't be 0
            if(titles.isEmpty())
                throwXibDataException(new IllegalStateException("Titles " +
                        "need to be initialized properly."));
            //Pages list can't be 0.
            if(pages.size() == 0)
                throwXibDataException(new IllegalStateException("Pages need" +
                        " to be initialized properly."));

            //boolean pagesPosition[] = new boolean[pages.size() + 1];
            IPageData page;

            //Verify pages specification
            for(int i=0; i < pages.size(); i++){
                page = pages.get(i);

                //Validate the page
                page.validate();
            }
            validate = true;
        }
        return true;
    }

    /**
     * Throw a new XibDataException with the cause from the given.
     * @param cause
     */
    private void throwXibDataException(Throwable cause){
        throw new XibDataException("Error in the internal data of the source, " +
                "in the ChapterData."
                , cause);
    }

}
